﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SeaBattle.UI.Web.Models;
using SeaBattle.Domain.Abstract.Services;
using SeaBattle.Domain.Concrete.Services;
using SeaBattle.UI.Web.Models.ChildActions;
using SeaBattle.Domain.Abstract;

namespace SeaBattle.UI.Web.Controllers
{
    public class RoomController : Controller
    {
        //
        // GET: /Room/

        private IPlayerRepository playerRepository;
        private IAccountService accountService;
        private IGameService gameService;

        public RoomController(IPlayerRepository playerRepository, IAccountService accountService, IGameService gameService)
        {
            this.accountService = accountService;
            this.gameService = gameService;
            this.playerRepository = playerRepository;

        }

        [Authorize]
        public ActionResult Index()
        {
            // Get player's account instance
            var player = playerRepository.GetPlayer(User.Identity.Name);

            // Ask player's account for balance value
            var balance = accountService.GetPlayerBalance(player);

            // Get game list

            var gameList = gameService.GetOpenedGames().Where(x=>x.Creator!=player);

            return View(new RoomViewModel{
                PlayerAccountBalance = balance,
                GameList = gameList              
            });
        }

        [ChildActionOnly]
        public ActionResult GameList(string playerLogin)
        {

            var player = playerRepository.GetPlayer(playerLogin);

            var gameList = from game in gameService.GetParticipantsPlayerGames(player) where game.Opponent != null select game;

            return View("ChildActions/GameList", new GameListViewModel { Games = gameList, UserLogin = playerLogin });
        
        }


    }
}
